package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TourGroup;
import com.ruoyi.system.domain.TourRoute;
import com.ruoyi.system.service.ITourRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("route")
public class TourRouteController {

    @Autowired
    private ITourRouteService routeService;

    @GetMapping("/list")
    public AjaxResult list() {
        try {
            List<TourRoute> routes = routeService.selectRoutes();
            return AjaxResult.success(routes);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询出错");
        }
    }

    @PostMapping("/city_list")
    public AjaxResult cityList(int cityId) {
        try {
            List<TourRoute> routes = routeService.selectRoutes(cityId);
            return AjaxResult.success(routes);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询出错");
        }
    }

    @PostMapping("/group_list")
    public AjaxResult groupList(int groupId) {
        try {
            TourGroup routes = routeService.selectGroupRoutes(groupId);
            return AjaxResult.success(routes);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询出错");
        }
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody TourRoute route) {
        try {
            int row = routeService.addRoute(route);
            if (row != 0) {
                int id = route.getId();
                return AjaxResult.success(id);
            }
            return AjaxResult.error("插入失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody TourRoute route) {
        try {
            int row = routeService.updateRoute(route);
            if (row != 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

    @PostMapping("/del")
    public AjaxResult del(@RequestParam int routeId) {
        try {
            int row = routeService.delRoute(routeId);
            if (row != 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("发生错误");
        }
    }

}
